package com.qianyitian.hope2.spider.fetcher;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qianyitian.hope2.sdk.BarSeries;
import com.qianyitian.hope2.sdk.Securities;
import com.qianyitian.hope2.spider.config.PropertyConfig;
import com.qianyitian.hope2.spider.external.MairuiClient;
import com.qianyitian.hope2.spider.external.XiaoXiongClient;
import com.qianyitian.hope2.spider.model.KLineInfo;
import com.qianyitian.hope2.spider.model.Stock;
import com.qianyitian.hope2.spider.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.LinkedList;
import java.util.List;


@Component("mairuiRetreiver")
public class MairuiRetreiver extends WebStockRetreiver {
    @Autowired
    PropertyConfig propertyConfig;
    @Autowired
    private MairuiClient mairuiClient;

    private int MAX_KLINE_NUMBER = 2500;

    @Override
    public Stock fetchStockInfo(Stock stock) throws IOException {
        String content = mairuiClient.getStockKlineDay(stock.getCode(), propertyConfig.getMairuiToken());
        JSONArray array = JSON.parseArray(content);
        int size = array.size();
        if (size == 0) {
            //获取失败，重新获取
            return null;
        }
        List<KLineInfo> kLineInfos = new LinkedList<KLineInfo>();
        int i = size >= MAX_KLINE_NUMBER ? size - MAX_KLINE_NUMBER : 0;
        for (; i < size; i++) {
            JSONObject kInfo = array.getJSONObject(i);
            String dateString = kInfo.getString("d");
            double open = kInfo.getDouble("o");
            double close = kInfo.getDouble("c");
            double low = kInfo.getDouble("l");
            double high = kInfo.getDouble("h");
            int volume = kInfo.getIntValue("v");
            double turnover = kInfo.getDouble("hs");

            KLineInfo daily = new KLineInfo();
            LocalDate date = LocalDate.parse(dateString, DateTimeFormatter.ISO_LOCAL_DATE);
            daily.setOpen(open);
            daily.setHigh(high);
            daily.setLow(low);
            daily.setClose(close);
            daily.setDate(date);
            daily.setVolume(volume);
            daily.setTurnoverRate(turnover);
            kLineInfos.add(daily);
        }
        stock.setkLineInfos(kLineInfos);
        stock.setkLineType(Stock.TYPE_DAILY);
        return stock;
    }

    @Override
    public Stock fetchIndexInfo(Stock stock) throws IOException {
        String content = mairuiClient.getIndexKlineDay(stock.getCode(), propertyConfig.getMairuiToken());
        JSONArray array = JSON.parseArray(content);
        int size = array.size();
        if (size == 0) {
            //获取失败，重新获取
            return null;
        }
        List<KLineInfo> kLineInfos = new LinkedList<KLineInfo>();
        int i = 0;
        for (; i < size; i++) {
            JSONObject kInfo = array.getJSONObject(i);
            String dateString = kInfo.getString("d");
            double open = kInfo.getDouble("o");
            double close = kInfo.getDouble("c");
            double low = kInfo.getDouble("l");
            double high = kInfo.getDouble("h");
            int volume = kInfo.getIntValue("v");
            double turnover = kInfo.getDouble("hs");

            KLineInfo daily = new KLineInfo();
            LocalDate date = LocalDate.parse(dateString, DateTimeFormatter.ISO_LOCAL_DATE);
            daily.setOpen(open);
            daily.setHigh(high);
            daily.setLow(low);
            daily.setClose(close);
            daily.setDate(date);
            daily.setVolume(volume);
            daily.setTurnoverRate(turnover);
            kLineInfos.add(daily);
        }
        stock.setkLineInfos(kLineInfos);
        stock.setkLineType(Stock.TYPE_DAILY);
        return stock;
    }

    @Override
    public String fetchFundsInfo(String code) throws IOException {
        return null;
    }

    public Securities fetchStockInfo(String code, String t) throws IOException {
        String content = mairuiClient.getStockKlineByPeriod(code, t, propertyConfig.getMairuiToken());
        JSONArray array = JSON.parseArray(content);
        int size = array.size();
        if (size == 0) {
            return null;
        }
        List<KLineInfo> kLineInfos = new LinkedList<KLineInfo>();
        int i = size >= MAX_KLINE_NUMBER ? size - MAX_KLINE_NUMBER : 0;
        BarSeries series = new BarSeries();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        for (; i < size; i++) {
            JSONObject kInfo = array.getJSONObject(i);
            String dateString = kInfo.getString("d");
            float open = kInfo.getFloat("o");
            float close = kInfo.getFloat("c");
            float low = kInfo.getFloat("l");
            float high = kInfo.getFloat("h");
            int volume = kInfo.getIntValue("v");
            float turnover = kInfo.getFloat("hs");
            LocalDateTime sf = LocalDateTime.parse(dateString, formatter);
            series.addBar( sf, open, high, low, close, turnover);
        }
        Securities securities = new Securities();
        securities.setBarSeries(series);
        securities.setCode(code);
        securities.setName("");
        return securities;
    }
}
